/*
#
#  Copyright 2011 Matthew Jannace
#
#  This file is part of RoboCode BlackHoleAgent
#
#  RoboCode BlackHoleAgent is free software: you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  RoboCode BlackHoleAgent is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with RoboCode BlackHoleAgent. If not, see
#  <http://www.gnu.org/licenses/>.
#
 */
package com.jannaceltd.TeamBlackHoleBeer.Modules.CustomEvents;

import com.jannaceltd.TeamBlackHoleBeer.BlackHole_Abstract;
import com.jannaceltd.TeamBlackHoleBeer.BlackHole_Interface;
import java.util.HashMap;
import robocode.Condition;
import robocode.CustomEvent;

/**
 * Utility methods to Processes CustomEvent Conditions
 * @author Matthew Jannace <mjannace@gmail.com>
 * @version 1.0
 */
public class CustomEventProcessor implements BlackHole_Interface {

    /**
     * Maintains pointer to Root Abstract class of the BlackHole Agent
     */
    private BlackHole_Abstract Agent;
    /**
     * An Internal Debugging Flag enables Debugging PrintStream Output
     */
    private boolean debug = false;
    /**
     * An Internal waterfall Flag that once thrown prevents any conditions below
     * the condition that threw the flag from processing.
     */
    private boolean violatedRespectedFlag = false;
    public HashMap<String, Long> Violator = null;

    /**
     * Creates A Custom Event Processor to check and respond to the statuses of
     * the four event conditions
     * @param Agent
     * @see Robot_DistanceBufferCondition_Violated
     * @see Bullet_DistanceBufferCondition_Respected
     * @see Bullet_DistanceBufferCondition_Violated
     * @see Robot_DistanceBufferCondition_Respected
     */
    public CustomEventProcessor(BlackHole_Abstract Agent) {
        this.Agent = Agent;
        this.Violator = new HashMap<String, Long>();
        Agent.addCustomEvent(new Robot_DistanceBufferCondition_Violated(Agent));
        Agent.addCustomEvent(new Bullet_DistanceBufferCondition_Respected(Agent));
        Agent.addCustomEvent(new Bullet_DistanceBufferCondition_Violated(Agent));
        Agent.addCustomEvent(new Robot_DistanceBufferCondition_Respected(Agent));

    }

    /**
     * Checks the passed CustomEvent to see if it one of the watched events.
     * If it is one of the watched events it throws a violatedRespectedFlag and
     * and takes necessary actions to resolve the specific event.
     * @param event
     * @see Robot_DistanceBufferCondition_Violated
     * @see Bullet_DistanceBufferCondition_Respected
     * @see Bullet_DistanceBufferCondition_Violated
     * @see Robot_DistanceBufferCondition_Respected
     */
    public void processEvents(CustomEvent event) {
        Condition condition = event.getCondition();
        if (condition instanceof Bullet_DistanceBufferCondition_Violated) {
            Agent.move.doEvasiveMove();
        } else if (condition instanceof Robot_DistanceBufferCondition_Violated) {
            Agent.move.doEvasiveMove();
        } else if (condition instanceof Bullet_DistanceBufferCondition_Respected) {
            if (Agent.move.getCurrentMovementState() == MovementState.Evasive) {
                Agent.move.doNormalMove();
            }
        } else if (condition instanceof Robot_DistanceBufferCondition_Respected) {
            if (Agent.move.getCurrentMovementState() == MovementState.Evasive) {
                Agent.move.doNormalMove();
            }
        }
    }

    /**
     * Returns the status  of the violatedRespectedFlag
     * @return boolean violatedRespectedFlag
     */
    public boolean isViolatedRespectedFlag() {
        return violatedRespectedFlag;
    }

    /**
     * Sets the Value of the violatedRespectedFlag
     * @param violatedRespectedFlag
     */
    public void setViolatedRespectedFlag(boolean violatedRespectedFlag) {
        this.violatedRespectedFlag = violatedRespectedFlag;
    }
}
